package com.china08.yxyapi.api.manage.tec.office.website.article;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.sws.SwsSection;
import com.china08.yxyapi.entity.mg.sws.SwsArticle;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.SwsSectionRepository;
import com.china08.yxyapi.repository.mg.SwsArticleRepository;
import com.china08.yxyapi.util.DateToolUtils;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class SwsTeaArticleListController extends YxyApiBaseController {

    @Autowired
    SwsArticleRepository swsArticleRepository;

    @Autowired
    SwsSectionRepository swsSectionRepository;

    @ApiOperation("获取栏目")
    @RequestMapping(value = { "/api_yw/manage/teaching/article/list",
            "/api_yw/manage/tec/office/website/article/list" }, method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Page<RespModel> precess(HttpServletRequest request, HttpServletResponse response,
            @RequestBody ReqModel reqModel) {
        String schoolId = getProfile(request, response).getSchoolId();
        String senderId = getProfile(request, response).getId();
        String section1Id = StringUtils.equals("all", reqModel.section1Id) ? "" : reqModel.section1Id;
        String section2Id = StringUtils.equals("all", reqModel.section2Id) ? "" : reqModel.section2Id;
        String keyWord = reqModel.keyWord;
        Page<SwsArticle> page = null;
        Pageable pageable = reqModel.getPageable(Direction.DESC, "publishDate");
        if (StringUtils.isNotBlank(section2Id) && StringUtils.isNotBlank(keyWord)) {
            page = swsArticleRepository.findByOwnerSchoolIdAndSenderIdAndSection2IdAndTitleContaining(schoolId,
                    senderId, section2Id, keyWord, pageable);
        } else if (StringUtils.isNotBlank(section2Id)) {
            page = swsArticleRepository.findByOwnerSchoolIdAndSenderIdAndSection2Id(schoolId, senderId, section2Id,
                    pageable);
        } else if (StringUtils.isNotBlank(section1Id) && StringUtils.isNotBlank(keyWord)) {
            page = swsArticleRepository.findByOwnerSchoolIdAndSenderIdAndSection1IdAndTitleContaining(schoolId,
                    senderId, section1Id, keyWord, pageable);
        } else if (StringUtils.isNotBlank(section1Id)) {
            page = swsArticleRepository.findByOwnerSchoolIdAndSenderIdAndSection1Id(schoolId, senderId, section1Id,
                    pageable);
        } else if (StringUtils.isNotBlank(keyWord)) {
            page = swsArticleRepository.findByOwnerSchoolIdAndSenderIdAndTitleContaining(schoolId, senderId, keyWord,
                    pageable);
        } else {
            page = swsArticleRepository.findByOwnerSchoolIdAndSenderId(schoolId, senderId, pageable);
        }
        List<RespModel> list = new ArrayList<SwsTeaArticleListController.RespModel>();
        for (SwsArticle swsArticle : page) {
            RespModel model = new RespModel();
            model.id = swsArticle.getId();
            Date publishDate = swsArticle.getPublishDate();
            String publishDateStr = publishDate != null
                    ? DateToolUtils.format(publishDate, DateToolUtils.FORMAT_SHORT_YMD) : "";
            model.publishDate = publishDateStr;
            model.title = swsArticle.getTitle();
            String section = "";
            String section1IdArticle = swsArticle.getSection1Id();
            if (StringUtils.isNotBlank(section1IdArticle)) {
                SwsSection sectionArticle = swsSectionRepository.findById(section1IdArticle);
                if (sectionArticle != null) {
                    section = sectionArticle.getSectionName();
                }
            }
            String section2IdArticle = swsArticle.getSection2Id();
            if (StringUtils.isNotBlank(section2IdArticle)) {
                SwsSection sectionArticle = swsSectionRepository.findById(section2IdArticle);
                if (sectionArticle != null) {
                    if (StringUtils.isNoneBlank(section)) {
                        section = section + " - ";
                    }
                    section = section + sectionArticle.getSectionName();
                }
            }
            model.section = section;
            model.author = swsArticle.getAuthor();
            model.auditStatus = swsArticle.getAuditStatus();
            model.section1Id = swsArticle.getSection1Id();
            model.section2Id = swsArticle.getSection2Id();
            list.add(model);
        }
        Page<RespModel> respPage = new PageImpl<SwsTeaArticleListController.RespModel>(list, pageable,
                page.getTotalElements());
        return respPage;
    }

    protected static class ReqModel extends YxyApiPageReqModel {
        public String section1Id;
        public String section2Id;
        public String keyWord;

        @Override
        public void autoCheck() {
        }
    }

    protected static class RespModel extends YxyApiRespModel {
        public String id;
        public String publishDate;
        public String title;
        public String section;
        public String author;
        public Integer auditStatus;
        public String section1Id;
        public String section2Id;
    }
}
